package pers.corvey.studyjava.model.form;

import java.io.File;
import java.io.IOException;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import pers.corvey.studyjava.MyConstant;
import pers.corvey.studyjava.model.Chapter;
import pers.corvey.studyjava.util.FileUtils;
import pers.corvey.studyjava.util.ToStringUtils;

public class ChapterForm implements MyValidatable {

    @NotNull(message="课程编号不能为空")
    private String courseId;
    
    @NotNull(message="章节标题不能为空")
    @Size(max=Chapter.TITLE_MAX_LEN, message="章节标题长度不能超过20个汉字")
    private String title;
    
    private String description;
    
    @NotNull(message="章节内容不能为空")
    private String content;
    
    public ChapterForm() {}

    public Chapter toChapter() throws IOException {
        Chapter chapter = new Chapter();
        chapter.setTitle(getTitle());
        chapter.setDescription(getDescription());
        chapter.setCourseId(getCourseId());
        File file = FileUtils.saveString(MyConstant.DOC_FILE_PATH, content, "md");
        chapter.setFilePath(MyConstant.DOC_FILE_PATH + file.getName());
        return chapter;
    }
    
    public String getCourseId() {
        return courseId;
    }

    public void setCourseId(String courseId) {
        this.courseId = courseId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String decription) {
        this.description = decription;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
    
    @Override
    public String toString() {
        return ToStringUtils.objectToString(this);
    }
}
